{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os  \n",
    "import random  \n",
    "  \n",
    "trainval_percent = 0.8\n",
    "train_percent = 0.8  \n",
    "xmlfilepath = 'Annotations'  \n",
    "txtsavepath = 'ImageSets\\Main'  \n",
    "total_xml = os.listdir(xmlfilepath)  \n",
    "  \n",
    "num=len(total_xml)  \n",
    "list=range(num)  \n",
    "tv=int(num*trainval_percent)  \n",
    "tr=int(tv*train_percent)  \n",
    "trainval= random.sample(list,tv)  \n",
    "train=random.sample(trainval,tr)  \n",
    "  \n",
    "ftrainval = open('ImageSets/Main/trainval.txt', 'w')  \n",
    "ftest = open('ImageSets/Main/test.txt', 'w')  \n",
    "ftrain = open('ImageSets/Main/train.txt', 'w')  \n",
    "fval = open('ImageSets/Main/val.txt', 'w')  \n",
    "  \n",
    "for i  in list:  \n",
    "    name=total_xml[i][:-4]+'\\n'  \n",
    "    if i in trainval:  \n",
    "        ftrainval.write(name)  \n",
    "        if i in train:  \n",
    "            ftrain.write(name)  \n",
    "        else:  \n",
    "            fval.write(name)  \n",
    "    else:  \n",
    "        ftest.write(name)  \n",
    "  \n",
    "ftrainval.close()  \n",
    "ftrain.close()  \n",
    "fval.close()  \n",
    "ftest.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
